/**    
 * @{#} MulitLevelDataChooseActivity.java Create on 2014-7-2 下午2:48:48    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import cn.skinapp.R;

import com.evan.common.adapter.BaseAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.entity.Hospital;
import com.smiier.skin.net.entity.Hospital.AreaHospital;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class MulitLevelDataChooseActivity extends BasicActivity implements
		OnItemClickListener {

	ListView list_common_data;

	private String url, title, param;
	private boolean isUpload = false;
	private String dataLeve;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_data_choose);

		init();
		list_common_data = (ListView) findViewById(R.id.list_common_data);
		String[] datas = getIntent().getStringArrayExtra(Constant.IDENTITY_KEY);
		url = datas[0];
		title = datas[1];
		param = datas[2];

		// 数据两层
		dataLeve = getIntent().getStringExtra(Constant.IDENTITY_KEY_LEVEL_2);

		setNavTitle(title);

		if (!CommonUtility.isNull(url) && url.contains("[")) {
			isUpload = true;
			try {
				o(new JSONArray(url));

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			/*
			 * ArrayList<CacheCommon> caches = dbUtil.queryCache(param); if
			 * (caches.size() > 0) { String data = caches.get(0).data; try {
			 * o(new JSONArray(data)); } catch (Exception e) {
			 * e.printStackTrace(); } } else { new RequestTask(activity, url,
			 * new RequestHandler(activity, new HandlerCallback() {
			 * 
			 * @Override public void callback(Object o) { if
			 * (!CommonUtility.isNull(o)) { try { JSONArray array = new
			 * JSONArray( o.toString()); o(array); dbUtil.saveOrUpdate(param,
			 * o); } catch (Exception e) { e.printStackTrace(); } } } }),
			 * RequestTask.CHAR).execute();
			 */

			// }
			String str = CommonUtility.getStringToBean(
					new Hospital(), "Hospital.txt", getContext());
			JSONArray array;
			try {
				array = new JSONArray(str);
				o(array);
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}
	}

	void o(JSONArray array) {
		MulitLevelCommonDataAdapter adapter = new MulitLevelCommonDataAdapter(
				activity, array);
		list_common_data.setAdapter(adapter);
		list_common_data
				.setOnItemClickListener(MulitLevelDataChooseActivity.this);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		try {
			Intent intent = null;
			JSONObject object = (JSONObject) view.getTag();
			// 表示已经到最后一层
			if (isUpload || !CommonUtility.isNull(dataLeve)) {
				intent = getIntent().setClass(activity,
						CommonDataChooseActivity.class);
				String[] datas = { object.getString("Value"),
						object.getString("Key"), param,
						CommonDataChooseActivity.TYPE_FIXATION,
						CommonDataChooseActivity.SINGLECHOOSE, "" };
				intent.putExtra(Constant.IDENTITY_KEY, datas);
				if (param.contains(Constant.SPLIT)) {
					intent.putExtra(
							Constant.IDENTITY_KEY_PROVINCE_CITY_COUNTRY, title
									+ Constant.SPLIT + object.getString("Key"));
				}
				startActivityForResult(intent, 0);
			} else {
				intent = getIntent().setClass(activity,
						MulitLevelDataChooseActivity.class);
				String[] datas = { object.getString("Value"),
						object.getString("Key"), param };
				intent.putExtra(Constant.IDENTITY_KEY, datas);
				startActivityForResult(intent, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		setResult(resultCode, data);
		finish();
	}

	public static class MulitLevelCommonDataAdapter extends BaseAdapter {

		public MulitLevelCommonDataAdapter(Activity activity, JSONArray array) {
			super(activity, array);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = views[position];
			if (!CommonUtility.isNull(convertView)) {
				return convertView;
			}
			JSONObject object = (JSONObject) getItem(position);
			convertView = inflater.inflate(R.layout.item_common_data, null);
			CheckBox checkBox = (CheckBox) convertView
					.findViewById(R.id.check_common);
			checkBox.setVisibility(View.GONE);
			TextView name = (TextView) convertView.findViewById(R.id.text_name);
			convertView.setTag(object);
			try {

				name.setText(object.getString("Key"));

			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		@Override
		public Object getItem(int position) {
			try {
				return array.getJSONObject(position);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
